<?php

namespace App\Constants;

use App\Constants\AbstractEnum;

/**
 * 文件状态枚举
 * 定义文件在系统中的各种状态
 */
class FileStatusEnum extends AbstractEnum
{
    // 文件状态常量
    public const UPLOADING = 'uploading';     // 上传中
    public const UPLOADED = 'uploaded';       // 已上传
    public const PROCESSING = 'processing';   // 处理中
    public const PROCESSED = 'processed';     // 已处理
    public const ACTIVE = 'active';           // 活跃
    public const INACTIVE = 'inactive';       // 非活跃
    public const ARCHIVED = 'archived';       // 已归档
    public const DELETED = 'deleted';         // 已删除
    public const FAILED = 'failed';           // 失败
    public const QUARANTINED = 'quarantined'; // 隔离中（安全检查）
    public const BLOCKED = 'blocked';         // 已阻止

    /**
     * 获取文件状态标签
     */
    public static function getLabels(): array
    {
        return [
            self::UPLOADING => '上传中',
            self::UPLOADED => '已上传',
            self::PROCESSING => '处理中',
            self::PROCESSED => '已处理',
            self::ACTIVE => '活跃',
            self::INACTIVE => '非活跃',
            self::ARCHIVED => '已归档',
            self::DELETED => '已删除',
            self::FAILED => '失败',
            self::QUARANTINED => '隔离中',
            self::BLOCKED => '已阻止',
        ];
    }

    /**
     * 获取文件状态颜色
     */
    public static function getColors(): array
    {
        return [
            self::UPLOADING => '#17A2B8',    // 青色
            self::UPLOADED => '#28A745',     // 绿色
            self::PROCESSING => '#FFC107',   // 黄色
            self::PROCESSED => '#28A745',    // 绿色
            self::ACTIVE => '#28A745',       // 绿色
            self::INACTIVE => '#6C757D',     // 灰色
            self::ARCHIVED => '#6F42C1',     // 紫色
            self::DELETED => '#DC3545',      // 红色
            self::FAILED => '#DC3545',       // 红色
            self::QUARANTINED => '#FFC107',  // 黄色
            self::BLOCKED => '#DC3545',      // 红色
        ];
    }

    /**
     * 获取文件状态图标
     */
    public static function getIcons(): array
    {
        return [
            self::UPLOADING => 'upload',
            self::UPLOADED => 'check-circle',
            self::PROCESSING => 'spinner',
            self::PROCESSED => 'check-double',
            self::ACTIVE => 'eye',
            self::INACTIVE => 'eye-slash',
            self::ARCHIVED => 'archive',
            self::DELETED => 'trash',
            self::FAILED => 'times-circle',
            self::QUARANTINED => 'shield-alt',
            self::BLOCKED => 'ban',
        ];
    }

    /**
     * 检查状态是否为活跃状态
     */
    public static function isActive(string $status): bool
    {
        return in_array($status, [self::ACTIVE, self::UPLOADED, self::PROCESSED]);
    }

    /**
     * 检查状态是否为非活跃状态
     */
    public static function isInactive(string $status): bool
    {
        return in_array($status, [self::INACTIVE, self::ARCHIVED, self::DELETED]);
    }

    /**
     * 检查状态是否为错误状态
     */
    public static function isError(string $status): bool
    {
        return in_array($status, [self::FAILED, self::BLOCKED]);
    }

    /**
     * 检查状态是否为处理中状态
     */
    public static function isProcessing(string $status): bool
    {
        return in_array($status, [self::UPLOADING, self::PROCESSING]);
    }

    /**
     * 获取可用的状态转换
     */
    public static function getAvailableTransitions(string $currentStatus): array
    {
        $transitions = [
            self::UPLOADING => [self::UPLOADED, self::FAILED],
            self::UPLOADED => [self::PROCESSING, self::ACTIVE, self::FAILED, self::QUARANTINED],
            self::PROCESSING => [self::PROCESSED, self::FAILED],
            self::PROCESSED => [self::ACTIVE, self::FAILED],
            self::ACTIVE => [self::INACTIVE, self::ARCHIVED, self::DELETED],
            self::INACTIVE => [self::ACTIVE, self::ARCHIVED, self::DELETED],
            self::ARCHIVED => [self::ACTIVE, self::DELETED],
            self::DELETED => [], // 已删除状态不可逆转
            self::FAILED => [self::UPLOADED, self::DELETED], // 可重新上传或删除
            self::QUARANTINED => [self::ACTIVE, self::BLOCKED, self::DELETED],
            self::BLOCKED => [self::ACTIVE, self::DELETED], // 可解封或删除
        ];

        return $transitions[$currentStatus] ?? [];
    }

    /**
     * 检查状态转换是否有效
     */
    public static function isValidTransition(string $fromStatus, string $toStatus): bool
    {
        $availableTransitions = self::getAvailableTransitions($fromStatus);
        return in_array($toStatus, $availableTransitions);
    }

    /**
     * 获取状态变更的日志级别
     */
    public static function getLogLevel(string $status): string
    {
        $errorStatuses = [self::FAILED, self::BLOCKED];
        $warningStatuses = [self::QUARANTINED, self::DELETED];
        $infoStatuses = [self::UPLOADING, self::UPLOADED, self::PROCESSING, self::PROCESSED, self::ACTIVE, self::INACTIVE, self::ARCHIVED];

        if (in_array($status, $errorStatuses)) {
            return 'error';
        } elseif (in_array($status, $warningStatuses)) {
            return 'warning';
        } elseif (in_array($status, $infoStatuses)) {
            return 'info';
        }

        return 'info';
    }

    /**
     * 获取状态变更的描述
     */
    public static function getTransitionDescription(string $fromStatus, string $toStatus): string
    {
        $descriptions = [
            self::UPLOADING . '_' . self::UPLOADED => '文件上传完成',
            self::UPLOADING . '_' . self::FAILED => '文件上传失败',
            self::UPLOADED . '_' . self::PROCESSING => '开始处理文件',
            self::UPLOADED . '_' . self::ACTIVE => '文件直接设为活跃状态',
            self::UPLOADED . '_' . self::QUARANTINED => '文件进入安全隔离',
            self::PROCESSING . '_' . self::PROCESSED => '文件处理完成',
            self::PROCESSING . '_' . self::FAILED => '文件处理失败',
            self::PROCESSED . '_' . self::ACTIVE => '文件设为活跃状态',
            self::ACTIVE . '_' . self::INACTIVE => '文件设为非活跃状态',
            self::ACTIVE . '_' . self::ARCHIVED => '文件已归档',
            self::ACTIVE . '_' . self::DELETED => '文件已删除',
            self::INACTIVE . '_' . self::ACTIVE => '文件重新激活',
            self::ARCHIVED . '_' . self::ACTIVE => '文件从归档中恢复',
            self::FAILED . '_' . self::UPLOADED => '文件重新上传',
            self::QUARANTINED . '_' . self::ACTIVE => '文件通过安全检查',
            self::QUARANTINED . '_' . self::BLOCKED => '文件被阻止',
            self::BLOCKED . '_' . self::ACTIVE => '文件解封',
        ];

        $key = $fromStatus . '_' . $toStatus;
        return $descriptions[$key] ?? "文件状态从 {$fromStatus} 变更为 {$toStatus}";
    }
}
